package org.asiainfo.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.flowable.service.WfCategoryService;
import org.asiainfo.flowable.domain.vo.WfCategoryVo;
import org.asiainfo.flowable.domain.bo.WfCategoryBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 流程分类表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wfCategory")
public class WfCategoryController extends BaseController {

    private final WfCategoryService wfCategoryService;

    /**
     * 分页查询
     *
     * @param wfCategoryBo 筛选条件
     * @param pageQuery    分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:category:list")
    @GetMapping("/list")
    public TableDataInfo<WfCategoryVo> list(WfCategoryBo wfCategoryBo, PageQuery pageQuery) {
        return wfCategoryService.queryPageList(wfCategoryBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param categoryId 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:category:query")
    @GetMapping("/{categoryId}")
    public R<WfCategoryVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("categoryId") Long categoryId) {
        return R.ok(wfCategoryService.queryById(categoryId));
    }

    /**
     * 新增数据
     *
     * @param wfCategoryBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:category:add")
    @Log(title = "流程分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WfCategoryBo wfCategoryBo) {
        if (wfCategoryService.checkCategoryCodeUnique(wfCategoryBo)) {
            return R.fail("新增流程分类" + wfCategoryBo.getCategoryName() + "失败，流程分类编码已存在");
        }
        return toAjax(wfCategoryService.insertByBo(wfCategoryBo));
    }

    /**
     * 编辑数据
     *
     * @param wfCategoryBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:category:edit")
    @Log(title = "流程分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WfCategoryBo wfCategoryBo) {
        if (wfCategoryService.checkCategoryCodeUnique(wfCategoryBo)) {
            return R.fail("修改流程分类" + wfCategoryBo.getCategoryName() + "失败，流程分类编码已存在");
        }
        return toAjax(wfCategoryService.updateByBo(wfCategoryBo));
    }

    /**
     * 删除数据
     *
     * @param categoryIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:category:remove")
    @Log(title = "流程分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long[] categoryIds) {
        return toAjax(wfCategoryService.deleteWithValidByIds(List.of(categoryIds)));
    }

    /**
     * 获取所有流程分类
     *
     * @param wfCategoryBo 筛选条件
     * @return 查询结果
     */
    @SaCheckPermission("system:category:list")
    @GetMapping("/listAll")
    public R<List<WfCategoryVo>> listAll(WfCategoryBo wfCategoryBo) {
        return R.ok(wfCategoryService.queryList(wfCategoryBo));
    }
}

